package com.hyzx.qbasic.admin.domain.service.impl;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.common.util.OperationResUtils;
import com.hyzx.qbasic.admin.domain.repository.ISysDataSetRepository;
import com.hyzx.qbasic.admin.domain.service.ISysDataSetService;
import com.hyzx.qbasic.admin.model.po.SysDataSetPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 数据集管理 服务实现类
 *
 * @author 黄江华
 * @since 2019-04-09
 */

@Service
public class SysDataSetServiceImpl implements ISysDataSetService {
    @Autowired
    private ISysDataSetRepository sysDataSetRepository;

    @Override
    public PageInfo<SysDataSetPO> getPagedSysDataSetList(GetSysDataSetListReq req) {
        PageInfo<SysDataSetPO> sysDataSetList = sysDataSetRepository.getPagedSysDataSetList(
                req.getPageNum(), req.getPageSize(), req.getDatasetName());

        return sysDataSetList;
    }

    @Override
    public SysDataSetPO getSysDataSetInfoById(GetSysDataSetInfoReq req) {
        return sysDataSetRepository.getSysDataSetById(req.getDatasetId());
    }

    @Override
    public SysDataSetPO getSysDataSetInfoByName(String datasetName) {
        return sysDataSetRepository.getSysDataSetByName(datasetName);
    }

    @Override
    public BaseOperationRes addSysDataSet(AddSysDataSetReq req) {
        //检查数据集名称是否已存在
        SysDataSetPO existedSysDataSet = sysDataSetRepository.getSysDataSetByName(req.getDatasetName());
        if (existedSysDataSet != null)
            return OperationResUtils.fail("数据集名称已存在");

        SysDataSetPO sysDataSet = new SysDataSetPO();
        sysDataSet.setDatasetName(req.getDatasetName());
        sysDataSet.setSqlString(req.getSqlString());
        sysDataSet.setRemark(req.getRemark());
        sysDataSet.setCreateTime(new Date());
        sysDataSet.setUpdateTime(new Date());

        boolean success = sysDataSetRepository.addSysDataSet(sysDataSet);
        if (!success)
            return OperationResUtils.fail("新增失败");

        return OperationResUtils.success();
    }

    @Override
    public BaseOperationRes editSysDataSet(EditSysDataSetReq req) {
        //检查记录是否存在
        SysDataSetPO existedSysDataSet = sysDataSetRepository.getSysDataSetById(req.getDatasetId());
        if (existedSysDataSet == null)
            return OperationResUtils.fail("记录不存在");

        //检查数据集名称是否已存在
        if (!existedSysDataSet.getDatasetName().equalsIgnoreCase(req.getDatasetName())) {
            existedSysDataSet = sysDataSetRepository.getSysDataSetByName(req.getDatasetName());
            if (existedSysDataSet != null)
                return OperationResUtils.fail("数据集名称已存在");
        }

        SysDataSetPO sysDataSet = new SysDataSetPO();
        sysDataSet.setDatasetId(req.getDatasetId());
        sysDataSet.setDatasetName(req.getDatasetName());
        sysDataSet.setSqlString(req.getSqlString());
        sysDataSet.setRemark(req.getRemark());
        sysDataSet.setUpdateTime(new Date());

        boolean success = sysDataSetRepository.editSysDataSet(sysDataSet);
        if (!success)
            return OperationResUtils.fail("编辑失败");

        return OperationResUtils.success();
    }

    @Override
    public BaseOperationRes deleteSysDataSet(DeleteSysDataSetReq req) {
        boolean success = sysDataSetRepository.deleteSysDataSetByIds(req.getDatasetIdList());
        if (!success)
            return OperationResUtils.fail("删除失败");

        return OperationResUtils.success();
    }
}